<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2017 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------

namespace app\admin\controller;
use think\Controller;
use think\Session;
use app\admin\model\MenuRoleModel;
use app\admin\model\MenuModel;

class Base extends Controller
{
	public function _initialize()
    {
        // 获取当前模块名称
        $module = '';
        $dispatch = request()->dispatch();
        if (isset($dispatch['module'])) {
            $module = $dispatch['module'][0];
        }

        // 获取入口目录
        $base_file = request()->baseFile();
        $base_dir  = substr($base_file, 0, strripos($base_file, '/') + 1);
        define('PUBLIC_PATH', $base_dir);
		define('MOUBLE_NAME', $module);
		define('TOKEN', md5('admin.login.user'));
		// 视图输出字符串内容替换
        $PUBLIC_STATIC_SRC = [
            // 静态资源目录
            '__STATIC__'    => PUBLIC_PATH. 'static',
            // 文件上传目录
            '__UPLOADS__'   => PUBLIC_PATH. 'uploads',
            // 后台CSS目录
            '__ADMIN_CSS__' => PUBLIC_PATH. 'static/admin/css',
            // 后台JS目录
            '__ADMIN_JS__'  => PUBLIC_PATH. 'static/admin/js',
            // 后台IMG目录
            '__ADMIN_IMG__' => PUBLIC_PATH. 'static/admin/images',
			// 后台LAYUI目录
            '__ADMIN_LAY__' => PUBLIC_PATH. 'static/admin/layui',
        ];
		$this->assign('MOUBLE_NAME',$module);
		$this->assign('PUBLIC_STATIC_SRC',$PUBLIC_STATIC_SRC);
		//Request()->module();->controller();action());
		$controller = Request()->controller();
		$action = Request()->action();
		if(empty(Session::get('admin_id'))){
			if(($controller=='Index' && $action=='login') || ($controller=='Login' && $action=='logining')){
				
			}else{
				$this->success('请先登录', '/'.$module.'/index/login');
			}
		}else{
			$controllers = config('Controllers');
			$actions = config('Actions');
			if(in_array($controller,$controllers) && in_array($action,$actions)){
				
			}else{
				$role_where = array();
				$role_where['role_id'] = Session::get('role_id');
				$MenuModel = new MenuModel();
				$where = array();
				$where['controller'] = $controller;
				$where['action'] = $action;
				$menu_row = $MenuModel->getRow($where);
				if(!empty($menu_row)){
					$role_where['module_id'] = $menu_row['id'];
					$role_where['status'] = 0;
					$MenuRoleModel = new MenuRoleModel();
					$role_row = $MenuRoleModel->getRole($role_where);
					if(empty($role_row) && $role_where['role_id'] > 1){
						$this->error('没有权限，请与管理员联系！');
					}
				}else{
					$this->error('请求不合法！');
				}
			}
		}
    }
}
